<template>
  <div>
    <div class="search">
      <div class="left">
        <el-input v-model="input" placeholder="请输入内容"></el-input>
        <el-button
          plain
          icon="el-icon-search"
          class="btn1"
          @click="lookShop"
        ></el-button>
      </div>

      <el-button type="primary" class="btn2" @click="addShop">添加商品</el-button>
    </div>

    <el-table :data="goodsList" border style="width: 100%">
      <el-table-column prop="goods_id" label="商品id"> </el-table-column>
      <el-table-column prop="goods_name" label="商品名称" width="500">
      </el-table-column>
      <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
      <el-table-column prop="goods_number" label="商品数量"> </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量"> </el-table-column>
      <el-table-column prop="add_time" label="创建时间" width="100"> 
        <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
        </template>
      </el-table-column>

      <el-table-column prop="cz" label="操作" width="200">
        <template slot-scope="scope">
          <el-button class="btn3" type="primary" icon="el-icon-edit"
            >编辑</el-button
          >

          <el-button
            class="btn3"
            type="danger"
            icon="el-icon-delete"
            @click="shanchu(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="fenye"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="shopInfo.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="shopInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getGoodsList, SearchGood, Deletegood } from "../../api";
export default {
  name: "GoodsList",
  data() {
    return {
      input: "",
      goodsList: [],
      currentPage4: 1,
      total: 0,
      shopInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
    };
  },
  methods: {
    //分页
    handleSizeChange(val) {
      this.shopInfo.pagesize = val;
      getGoodsList(this.shopInfo).then((res) => {
        this.goodsList = res.data.data.goods;
        this.total = res.data.data.total;
      });
    },
    handleCurrentChange(val) {
      this.shopInfo.pagenum = val;
      getGoodsList(this.shopInfo).then((res) => {
        this.goodsList = res.data.data.goods;
        this.total = res.data.data.total;
      });
    },

    //搜素
    lookShop() {
      if (this.input) {
        SearchGood(this.input).then((res) => {
          if (res.data.meta.status == 200) {
            this.$message({
              message: res.data.meta.msg,
              type: "success",
            });
            this.goodsList = [res.data.data];
            this.total = 1;
          } else {
            this.$message({
              message: res.data.meta.msg,
              type: "error",
            });
          }
        });
      } else {
        getGoodsList(this.shopInfo).then((res) => {
          this.goodsList = res.data.data.goods;
          this.total = res.data.data.total;
        });
      }
    },

    //删除
    shanchu(row) {
      Deletegood(row.goods_id).then((res) => {
        if (res.data.meta.status == 200) {
          this.$message({
            message: res.data.meta.msg,
            type: "success",
          });
          getGoodsList(this.shopInfo).then((res) => {
            this.goodsList = res.data.data.goods;
            this.total = res.data.data.total;
          });
        }
      });
    },

    //添加
    addShop(){
        this.$router.push('/admin/add')
    }
  },
  created() {
    getGoodsList(this.shopInfo).then((res) => {
      this.goodsList = res.data.data.goods;
      this.total = res.data.data.total;
    });
  },
};
</script>

<style scoped lang="less">
.search {
  display: flex;
  height: 32px;
  margin-bottom: 20px;
}
.left {
  width: 30%;
  display: flex;
  height: 32px;
  /deep/.el-input__inner {
    height: 32px;
  }
  .btn1 {
    color: #dcdfe6;
  }
  .btn1:hover,
  .btn1:target,
  .btn1:focus {
    border-color: #dcdfe6;
  }
}
.btn2 {
  width: 88px;
  margin-left: 20px;
}
.btn3 {
  width: 64px;
  height: 24px;
  font-size: 12px;
  padding: 0;
}
</style>